/*
 * @Description: 连接数据库
 * @Author: zheng yiliang
 * @Date: 2021-03-27 13:44:44
 * @LastEditTime: 2021-04-18 15:04:59
 * @LastEditors: zheng yiliang
 */

// 引入 mongoose 第三方模块
const mongoose = require('mongoose');
const consola = require('consola')
const { MONGODB_CONF } = require('../config/db')
const { host } = MONGODB_CONF
const autoIncrement = require('mongoose-auto-increment-fix');


// const { Schema, model } = mongoose
// exports.Schema = Schema

// 连接数据库
mongoose.connect(host, {
  useNewUrlParser: true,
  useUnifiedTopology: true,
  useFindAndModify: false,
  useCreateIndex: true
});
// 连接错误
mongoose.connection.on('error', error => {
  consola.warn('数据库连接失败!', error)
})
// 连接成功
mongoose.connection.once('open', () => {
  consola.ready('数据库连接成功!')
})
// 自增 ID 初始化
autoIncrement.initialize(mongoose.connection)
module.exports = mongoose
